<?php
namespace Admin\Controller;
use Com\Easemob;
/**
 * 投诉
 */
class HxController extends  AdminController{
    //好友投诉列表
    public function friendIndex(){
        $map['state'] = array('eq', '0');
        $result = $this->lists('user_complaint', $map);
        foreach ($result as $key => $value) {
            $result[$key]['name'] = M('member')->where('uid='.$value['uid'])->getField('nickname');
            $result[$key]['create_time'] = date('Y-m-d H:i:s', $value['create_time']);
        }
        $this->assign('result', $result);
        $this->display('friendIndex');
    }
    //获取好友聊天的详情
    public function friendInfo(){
        $id = I('id');
        $result = M('user_complaint')->where('id='.$id)->order('create_time asc')->find();
        $this->assign('result', $result);
        $this->display('friendInfo');
    }
    //好友处理投诉
    public function friendDispose(){
        $id = I('get.id');//
        $state = I('get.state');
        $complaint = M('user_complaint')->where('id='.$id)->find();
        if ($state == 2) {//不通过禁用该好友
            //获取被投诉人的名字
            $username = M('member')->where('uid='.$complaint['toid'])->getField('nickname');
            $data = array(
                'username' => $username,
                'create_time' => NOW_TIME,
                'type' => $complaint['type']
            );
            $result = $this->deactiveUser($username, $data);
            $resultCom = M('user_complaint')->where('id='.$id)->save(array('state'=>2));
            if ($result && $resultCom) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        } else {
            $result = M('user_complaint')->where('id='.$id)->save(array('state'=>1));
            if ($result) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
    }

    /*
     禁用用户账号
     */
    function deactiveUser($username, $data){
        $hx=new Easemob();
        $result = $hx->deactiveUser($username);
        if (!isset($result['error'])) {
            $res = M('user_disable')->add($data);
            if ($res) {
                return $res;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }
    /*
     解禁用户账号
     */
    function activeUser(){
        $username = I('username');
        $hx=new Easemob();
        $result = $hx->activeUser($username);
        if (!isset($result['error'])) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }
    /**
     * 用户禁用列表
     */
    public function deactiveList() {
        $result = M('user_disable')->order('create_time asc')->select();
        $this->assign('result', $result);
        $this->display('deactiveList');
    }
    //群投诉列表
    public function groupIndex(){
        $map['state'] = array('eq', '0');
        $result = $this->lists('group_complaint', $map);
        foreach ($result as $key => $value) {
            $result[$key]['name'] = M('member')->where('uid='.$value['uid'])->getField('nickname');
            $result[$key]['create_time'] = date('Y-m-d H:i:s', $value['create_time']);
        }
        $this->assign('result', $result);
        $this->display('groupIndex');
    }
    //获取群记录
    public function groupInfo(){
        $id = I('id');
        $result = M('group_complaint')->where('id='.$id)->order('create_time asc')->find();
        $this->assign('result', $result);
        $this->display('groupInfo');
    }
    //群处理投诉
    public function groupDispose(){
        $id = I('get.id');
        $type = I('get.state');
        $complaint = M('group_complaint')->where('id='.$id)->find();
        if ($type == 2) {//不通过解散群组
            //将关于该群组一切相关都删除
            M('group_group')->where('gid='.$complaint['group_id'])->delete();
            M('group_member')->where('gid='.$complaint['group_id'])->delete();
            M('group')->where('id='.$complaint['group_id'])->delete();
            $resultCom = M('group_complaint')->where('id='.$id)->save(array('type'=>2));
            $hx=new Easemob();
            //$result = $hx->deleteGroup($complaint['group_id']);
            if ($resultCom ) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        } else {
            $result = M('group_complaint')->where('eid='.$id)->save(array('type'=>1));
            if ($result) {
                $this->success('操作成功');
            } else {
                $this->error('操作失败');
            }
        }
    }
}